package com.lsh.oms.provider.service.status;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.status.FreightDto;
import com.lsh.oms.api.model.status.ModifyAddressInfoDto;
import com.lsh.oms.api.model.status.ModifyExtInfoDto;
import com.lsh.oms.api.model.status.ModifyFreightDto;
import com.lsh.oms.api.service.status.IModifyInfoService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.service.status.impl.ModifyInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * @author peter
 */
@Service(protocol = "rest")
@Path("order/modify")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class OrderInfoRestServiceImpl implements IModifyInfoService {

    private static Logger logger = LoggerFactory.getLogger(OrderInfoRestServiceImpl.class);

    @Autowired
    private ModifyInfoService modifyInfoService;

    /**
     * 修改地址
     * @param modifyAddressInfoDto
     * @return
     */
    @POST
    @Path("address_info")
    @Override
    public CommonResult<Boolean> modifyAddressInfo(ModifyAddressInfoDto modifyAddressInfoDto) {
        UUID uuid = UUID.randomUUID();
        logger.info("modifyAddressInfo uuid {},modifyAddressInfoDto {}.", uuid, modifyAddressInfoDto);

        try {
            Long code = modifyAddressInfoDto.getCode();
            Integer type = modifyAddressInfoDto.getType();
            String json = modifyAddressInfoDto.getJson();

            if (!StringUtils.hasText(json) || null == code || null == type) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "Request params is null!");
            }

            JSONObject addressInfo = JSONObject.parseObject(json);
            if (!modifyInfoService.checkAddressInfo(type, addressInfo)) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "Request params error!");
            }
            modifyInfoService.modifyAddressInfo(uuid.toString(), code, type, addressInfo);
        } catch (BusinessException e) {
            return new CommonResult(ReturnCode.REQUEST_FAIL, e.getMessage(), false);
        } catch (Throwable throwable) {
            logger.error(" [ModifyAddressInfo][{}][Modify address info error, error message:{}]", uuid.toString(), throwable.getMessage());
            return new CommonResult(ReturnCode.SYS_ERROR, "系统繁忙", false);
        }

        return new CommonResult(ReturnCode.REQUEST_SUCCESS, "success.", true);
    }

    /**
     * 修改ext信息
     * @param modifyExtInfoDto  参数
     * @return                  数据
     */
    @POST
    @Path("ext_info")
    @Override
    public CommonResult<Boolean> modifyExtInfo(ModifyExtInfoDto modifyExtInfoDto) {
        UUID uuid = UUID.randomUUID();
        logger.info("modifyExtInfo uuid {},modifyExtInfoDto {}", uuid, modifyExtInfoDto);

        try {

            Long code = modifyExtInfoDto.getCode();
            Integer type = modifyExtInfoDto.getType();
            String value = modifyExtInfoDto.getValue();

            if (!StringUtils.hasText(value) || code == null || type == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "Request params is null!");
            }

            if (!modifyInfoService.checkExtInfo(type, value)) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "Request params error!");
            }

            modifyInfoService.modifyExtInfo(uuid.toString(), code, type, value);
        } catch (BusinessException e) {
            return new CommonResult(ReturnCode.REQUEST_FAIL, e.getMessage(), false);
        } catch (Throwable throwable) {
            logger.error(" [ModifyExtInfo][{}][Modify ext info error, error message:{}]", uuid.toString(), throwable.getMessage());
            logger.error(throwable.getMessage(), throwable);
            return new CommonResult(ReturnCode.SYS_ERROR, "系统繁忙", false);
        }

        return new CommonResult(ReturnCode.REQUEST_SUCCESS, "success.", true);
    }

    /**
     * 修改运费
     *
     * @param modifyFreightDto 参数
     * @return                 结果
     */
    @POST
    @Path("freight")
    @Override
    public CommonResult<Boolean> modifyFreight(ModifyFreightDto modifyFreightDto) {

        try {
            logger.info("modifyFreightDto is {}", JSON.toJSONString(modifyFreightDto));
            if (CollectionUtils.isEmpty(modifyFreightDto.getFreightList())) {
                return new CommonResult(ReturnCode.REQUEST_FAIL, "freightList 参数不能为空", false);
            }
            for (FreightDto freightDto : modifyFreightDto.getFreightList()) {
                Long orderCode = freightDto.getOrderCode();
                BigDecimal freight = freightDto.getFreight();

                if (null == orderCode || orderCode == 0L) {
                    return new CommonResult(ReturnCode.REQUEST_FAIL, "orderCode 参数不能为空", false);
                }

                if (null == freight || freight.compareTo(BigDecimal.ZERO) < 0) {
                    return new CommonResult(ReturnCode.REQUEST_FAIL, "freight 参数不能为空", false);
                }
            }

            boolean updateFlag = this.modifyInfoService.modifyFreight(modifyFreightDto.getFreightList());

            if (true == updateFlag) {
                return new CommonResult(ReturnCode.REQUEST_SUCCESS, "success.", true);
            } else {
                return new CommonResult(ReturnCode.REQUEST_FAIL, "error", false);
            }
        } catch (BusinessException e) {
            return new CommonResult(ReturnCode.REQUEST_FAIL, e.getMessage(), false);
        } catch (Throwable throwable) {
            logger.error(" [modifyFreight][Modify error message:" + throwable.getMessage() + "]");
            logger.error(throwable.getMessage(), throwable);
            return new CommonResult(ReturnCode.SYS_ERROR, "系统繁忙", false);
        }
    }

}
